"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
from datetime import datetime
import time

import requests
from fake_useragent import UserAgent
from lxml import html

from operating_sql import SQLManager


class Listed:
    def __init__(self):
        self.hu_url = 'http://eid.csrc.gov.cn/101110/index.html'
        self.sh_url = 'http://eid.csrc.gov.cn/101810/index.html'
        # 三表类型 利润，负载，流量
        self.table_type = ["income", "balance", "cash_flow"]
        self.headers = {
            "User-Agent": UserAgent().ie
        }
        self.sql = SQLManager()
        self.xueqiu_headers = {
            'User-Agent': UserAgent().ie,
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Host": "stock.xueqiu.com",
            "Pragma": "no-cache",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "Cookie": "device_id=66c9ce413d8d7cb2336815ac6a38fd0d; s=c111ybndx0; xq_a_token=3242a6863ac15761c18a8469b89065b03bd5e164; xqat=3242a6863ac15761c18a8469b89065b03bd5e164; xq_r_token=729679220e12a2fbd19b15c94e6b7624c5ea8702; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTYwNDgwMzAzMSwiY3RtIjoxNjAyMjQxNTUxOTk1LCJjaWQiOiJkOWQwbjRBWnVwIn0.A7T2psev7vxcvYgD8XjFdhkN4CYElre_cFdjhcfmGql0FTQrVCMxGQw60XCaEs0Yx09LfqByuOlSKeh2MIqWjMYvz9i7LvuqreBtMqTR1UnRZX9uolIY6ufxYAX1Ic5MCj8jvtiiMc0zfRWkLW1J471v8BJEaZNl_6qGEvNKEXUA8vWoJPz7Y5k5-7HL7u53wf19r1tr4fz4G3DhGwuiMxV0_2V9i_Xbb810gKPO6nPHOWaeuFTOB2AsLIL916fqeC4A9jSPHl5PqxYqkmPc_5P10bGZTkpWjmn0C0sdpCTyl6V_ROycu-mf9E_cXltoPqH54zHxsNri15dySAEMJw; u=111602241594487; Hm_lvt_1db88642e346389874251b5a1eded6e3={}; Hm_lpvt_1db88642e346389874251b5a1eded6e3={}".format(
                str(time.time()).split(".")[:10], str(time.time()).split(".")[:10])
        }
        self.start_time = datetime.now().date()
        self.ins_list = []
        # 链接：cn 国内数据，us：美投
        self.xueqiu_url = "https://stock.xueqiu.com/v5/stock/finance/cn/{}.json?symbol={}&type={}&is_detail=true&count=5&timestamp="

    def get_html(self, url):
        for page in range(1, 50):
            html_ = requests.get(url.format(page), headers=self.headers).text
            parse_html = html.fromstring(html_)
            li_list = parse_html.xpath("//div[@id='txt']/table//tr")
            for li in li_list[1:]:
                ticker = li.xpath("./td[1]/a/text()")[0]
                company = li.xpath("./td[2]/text()")[0]
                title = li.xpath("./td[3]/a/text()")[0].strip()
                types = li.xpath("./td[5]/text()")[0].strip()
                announcement_time = datetime.strptime(li.xpath("./td[6]/text()")[0], '%Y-%m-%d').date()
                if announcement_time.__eq__(self.start_time):
                    print(ticker, company, title, types, announcement_time)
                    if ticker not in self.ins_list:
                        self.ins_list.append(ticker)
                else:
                    return

    def verification(self):
        print(self.ins_list)
        status = input("是否确认插入数据(y/n):")
        if status == "y":
            print("插入开始。。")
            self.ins_data()
        else:
            print("不插入")

    def ins_data(self):
        for data in self.ins_list:
            for table in self.table_type:
                for types in ["Q1", "Q2", "Q3", "Q4"]:
                    if types == "Q1":
                        report_type = "一季报"
                    elif types == "Q2":
                        report_type = "中报"
                    elif types == "Q3":
                        report_type = "三季报"
                    else:
                        report_type = "年报"
                    ticker = "SH" + data
                    json_ = requests.get(self.xueqiu_url.format(table, ticker, types),
                                         headers=self.xueqiu_headers).json()
                    if not json_["data"]["list"]:
                        ticker = "SZ" + data
                        json_ = requests.get(self.xueqiu_url.format(table, ticker, types),
                                             headers=self.xueqiu_headers).json()
                    if table == "income":
                        financials = "利润表"
                    elif table == "balance":
                        financials = "负债表"
                    else:
                        financials = "资金流量表"
                    for info in json_["data"]["list"]:
                        if info["report_name"][:4] > "2014":
                            for i in info:
                                if i == "report_date" or i == "report_name" or i == "ctime":
                                    continue
                                if info[i][0]:
                                    y_0 = info[i][0]
                                else:
                                    y_0 = ""
                                sql_cx = 'select * from xueqiu_data where company="%s" and report_type="%s" and financials="%s" and item="%s"' % (
                                    ticker, report_type, financials, i)
                                status = self.sql.get_tuple_one(sql_cx)

                                # 待完善，此处暂定y0
                                if status:
                                    update_sql = 'update xueqiu_data set y_0="%s" where  company="%s" and report_type="%s" and financials="%s" and item="%s"' % (
                                        y_0, ticker, report_type, financials, i)
                                    self.sql.run(update_sql)
                                    print("更新语句", ticker,report_type, financials, i, y_0)
                                else:
                                    ins_sql = 'insert into xueqiu_data(company,report_type,financials,item,y_0) values ("%s","%s","%s","%s","%s")' % (
                                        ticker, report_type, financials, i, y_0)
                                    self.sql.run(ins_sql)
                                    print("插入语句", ticker, report_type, financials, i, y_0)

    def run(self):
        self.get_html(self.sh_url)
        self.get_html(self.hu_url)
        self.verification()


if __name__ == '__main__':
    l = Listed()
    l.run()
